package cn.jly.boot.mp.cron.scheduler;

import cn.jly.boot.mp.cron.QuartzCronSchedulerJob;
import cn.jly.boot.mp.cron.QuartzCronSchedulerJob2;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

/**
 * @PackageName cn.jly.boot.mp.cron.scheduler
 * @ClassName CronSchedulerJob
 * @Description quartz scheduler
 * @Author 姬岚洋
 * @Date 2021/1/6 上午 9:42
 */
@Component
public class CronSchedulerJob {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    public void scheduleJobs() throws SchedulerException {
        final Scheduler scheduler = schedulerFactoryBean.getScheduler();
        schedule1(scheduler);
        schedule2(scheduler);
    }

    private void schedule1(Scheduler scheduler) throws SchedulerException {
        final JobDetail jobDetail = JobBuilder.newJob(QuartzCronSchedulerJob.class).withIdentity("job1", "group1").build();
        final CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/6 * * * * ?");
        final CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }
    private void schedule2(Scheduler scheduler) throws SchedulerException {
        final JobDetail jobDetail = JobBuilder.newJob(QuartzCronSchedulerJob2.class).withIdentity("job2", "group2").build();
        final CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/12 * * * * ?");
        final CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group2").withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }
}
